import { isNil, cloneDeep, random } from 'lodash';
import styleMap from './nodeStyle';
import G6 from '@antv/g6';
import { getSubordinateData } from './graphUtil';

/**
 * 路径布局描述
 * 1，默认仅展示根节点（以及与根节点之间相关联的节点）
 * 2，右击节点出现菜单
 *  2.1【展开】展开当前节点相关联的节点
 *  2.2【收起】收起当前节点相关联的节点
 *  2.3【标记】收起节点，节点如果被标记，则不收起
 */
export default class PathLayout {
  constructor(graph, options) {
    this.graph = graph;
    this.graphData = cloneDeep(options);
    this.currentNodes = [];
    this.specialNodes = []; // 存放路径节点
    this.nodesMap = {};
    this.init();
    console.log('PahtLayout=', this);
  }
  init() {
    this.createdNodesMap();
  }

  // 创建nodes map 集合
  createdNodesMap() {
    this.graphData.nodeList.forEach(o => (this.nodesMap[o.id] = o));
  }

  // 获取下级节点
  getSubordinateNodes(node) {
    const nodeList = [];
    this.graphData.edgeList.forEach(e => {
      const currentId = [e.source, e.target].find(o => node.id === o);
      if (currentId) {
        const id = [e.source, e.target].find(o => node.id !== o);
        if (isNil(this.nodesMap[id].level)) {
          const hasSpeial = this.specialNodes.find(o => this.nodesMap[id].id === o.id);
          !hasSpeial && nodeList.push(this.nodesMap[id]);
        }
      }
    });

    return nodeList;
  }
  // 移除下级节点
  removeSubordinateNodes() {}

  // 添加特殊节点
  addSpecialNodes(node) {
    this.specialNodes.push(node);
    console.log('添加特殊节点', this.specialNodes);
  }

  // 更新特殊节点
  updateSpecialNodes() {
    console.log('更新特殊节点');
  }

  // 处理节点
  processNodes(nodes) {
    nodes.forEach(node => {
      let config = {};
      //   node.x = node.x || random(50, 300);
      //   node.y = node.y || random(50, 300);
      if (node.center) {
        //中心点样式特殊处理
        node.size = 30;
        config = Object.assign({}, styleMap[node.class], styleMap['center']);
      } else if (node.subCenter) {
        //副中心点样式特殊处理
        node.size = 30;
        config = Object.assign({}, styleMap[node.class], styleMap['subCenter']);
      } else {
        config = styleMap[node.class] ? styleMap[node.class] : styleMap['default'];
      }
      Object.assign(node, config);
    });
  }

  //处理边
  processEdges(edges) {
    edges.forEach(edge => {
      edge.type = 'line';
    });
  }
  // 获取扩展节点
  extendNodes(node) {
    this.addSpecialNodes(node);
    const subordinateNodes = this.getSubordinateNodes(node).map(o => {
      // o.x = null;
      // o.y = null;
      return o;
    });
    const nodes = subordinateNodes;
    const edges = this.graphData.edgeList;
    this.processNodes(nodes);
    this.processEdges(edges);

    return {
      nodes,
      edges,
    };
  }

  // 获取关键节点
  getSpecialNodes() {
    this.processNodes(this.specialNodes);
    return this.specialNodes;
  }

  // 子图布局
  getSubgraphLayout(node, subordinateData = {}) {
    // 实例化布局
    const subgraphLayout = new G6.Layout['gForce']({
      center: [node.x, node.y],
      type: 'gForce',
      linkDistance: 200,
      nodeSpacing: 30,
      preventOverlap: true,
      nodeSize: 48,
      minMovement: 0.01,
      maxIteration: 1000,
      damping: 0.99,
      tick: () => {
        this.graph.refreshPositions();
      },
    });
    console.log('subordinateData', subordinateData);

    // 初始化布局，灌入子图数据
    subgraphLayout.init({
      nodes: subordinateData.nodes || [],
      edges: subordinateData.edges || [],
    });

    // 执行布局
    subgraphLayout.execute();
    console.log('subgraphLayout', subgraphLayout);

    // 图实例根据数据更新节点位置
    // graph.positionsAnimate();
    return subgraphLayout;
  }
}
